package org.inspiration.dudu.web.service.user.impl;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.user.mapper.UserMapper;
import org.inspiration.dudu.user.model.dto.UserFollowDTO;
import org.inspiration.dudu.user.model.vo.UserVO;
import org.inspiration.dudu.user.service.UserFollowService;
import org.inspiration.dudu.user.service.UserService;
import org.inspiration.dudu.web.service.user.UserFollowFacadeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户关注服务实现类
 * 提供用户关注和取消关注的相关操作
 */
@Service
@RequiredArgsConstructor
public class UserFollowFacadeServiceImpl implements UserFollowFacadeService {

    private final UserFollowService userFollowService;
    private final UserService userService;
    private final UserMapper userMapper;

    /**
     * 关注用户
     *
     * @param userFollowDTO 包含关注信息的 DTO 对象
     */
    @Override
    @Transactional
    public void followUser(UserFollowDTO userFollowDTO) {
        Long followerId = userFollowDTO.getFollowerId();
        Long followingId = userFollowDTO.getFollowingId();
        // 建立用户关注关系
        userFollowService.followUser(followerId, followingId);
        // 增加粉丝量
        userService.increaseFollowerCount(followingId);
        // 增加关注量
        userService.increaseFollowingCount(followerId);
    }

    /**
     * 取消关注用户
     *
     * @param userFollowDTO 包含取消关注信息的 DTO 对象
     */
    @Override
    @Transactional
    public void unfollowUser(UserFollowDTO userFollowDTO) {
        Long followerId = userFollowDTO.getFollowerId();
        Long followingId = userFollowDTO.getFollowingId();
        // 取消用户关注关系
        userFollowService.unfollowUser(followerId, followingId);
        // 减少粉丝量
        userService.decreaseFollowerCount(followingId);
        // 减少关注量
        userService.decreaseFollowingCount(followerId);
    }

    /**
     * 获取用户关注的用户ID列表
     *
     * @param followerId 关注者的用户ID
     * @return 关注的用户ID列表
     */
    @Override
    public List<UserVO> getFollowingUsers(Long followerId, Integer pageNum, Integer pageSize) {
        List<UserVO> res = new ArrayList<>();
        List<Long> ids = userFollowService.getFollowingUserIds(followerId, pageNum, pageSize);
        for(Long id : ids) {
            res.add(userService.getUserById(id));
        }
        return res;
    }

    /**
     * 获取用户的粉丝ID列表
     *
     * @param followingId 被关注者的用户ID
     * @return 粉丝的用户ID列表
     */
    @Override
    public List<UserVO> getFollowerUsers(Long followingId, Integer pageNum, Integer pageSize) {
        List<UserVO> res = new ArrayList<>();
        List<Long> ids = userFollowService.getFollowerUserIds(followingId, pageNum, pageSize);
        for(Long id : ids) {
            res.add(userService.getUserById(id));
        }
        return res;
    }

    /**
     * 检查用户是否已关注某个用户
     *
     * @param followerId 关注者的用户ID
     * @param followingId 被关注者的用户ID
     * @return 如果已关注返回true，否则返回false
     */
    @Override
    public Boolean isFollowing(Long followerId, Long followingId) {
        return userFollowService.isFollowing(followerId, followingId);
    }
}
